import { Component, Injector, ViewEncapsulation, ViewChild, AfterViewInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Http } from '@angular/http';
import { ScheduleServiceProxy, ScheduleListDto } from '@shared/service-proxies/service-proxies';
import { NotifyService } from '@abp/notify/notify.service';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { TokenAuthServiceProxy } from '@shared/service-proxies/service-proxies';
import { CreateOrEditScheduleModalComponent } from './create-or-edit-schedule-modal.component';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { DataTable } from 'primeng/components/datatable/datatable';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';

import * as moment from 'moment';
import { SelectItem } from 'primeng/components/common/selectitem';
import { Table } from 'primeng/table';
import { BoundEquipmentSchedulesModalComponent } from './bound-equipment-schedules-modal.component';

@Component({
    templateUrl: './schedule.component.html',
    animations: [appModuleAnimation()]
})

export class SchedulesComponent extends AppComponentBase {

    @ViewChild('createOrEditScheduleModal') createOrEditScheduleModal: CreateOrEditScheduleModalComponent;
    @ViewChild('boundEquipmentSchedulesModal') boundEquipmentSchedulesModal: BoundEquipmentSchedulesModalComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    allCols: any[];
    selectedCols: any[];

    //Filters
    advancedFiltersAreShown = false;
    filterText = '';
    workDays: boolean[] = [false, false, false, false, false, false, false];
    priority: number = undefined;
    validTimeStartBegin: moment.Moment = undefined;
    validTimeStartEnd: moment.Moment = undefined;
    validTimeEndBegin: moment.Moment = undefined;
    validTimeEndEnd: moment.Moment = undefined;
    columnOptions: SelectItem[];

    constructor(
        injector: Injector,
        private _http: Http,
        private _scheduleServiceProxy: ScheduleServiceProxy,
        private _notifyService: NotifyService,
        private _fileDownloadService: FileDownloadService,
        private _tokenAuth: TokenAuthServiceProxy,
        private _activatedRoute: ActivatedRoute
    ) {
        super(injector);
    }

    // tslint:disable-next-line:use-life-cycle-interface
    ngOnInit() {
        this.allCols = [
            { header: '工作日', hidden: false },
            { header: '早班', hidden: false },
            { header: '午班', hidden: false },
            { header: '晚班', hidden: false },
            { header: '开始时间', hidden: false },
            { header: '截止时间', hidden: false },
            { header: '优先级', hidden: false },
            { header: '备注', hidden: false }
        ];

        this.selectedCols = [];

        this.columnOptions = [];
        for (let i = 0; i < this.allCols.length; i++) {
            this.columnOptions.push({ label: this.allCols[i].header, value: this.allCols[i] });
            if (!this.allCols[i].hidden) {
                this.selectedCols.push(this.allCols[i]);
            }
        }
    }

    onColsChange(event: any) {
        for (let i = 0; i < this.allCols.length; i++) {

            let header = this.allCols[i].header;
            this.allCols[i].hidden = true;

            for (let j = 0; j < this.selectedCols.length; j++) {
                if (this.selectedCols[j].header === header) {
                    this.allCols[i].hidden = false;
                    continue;
                }
            }
        }
    }

    getSchedules(event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();

        let days: number[] = new Array<number>();
        this.workDays.forEach((val, idx) => {
            if (val) {
                days.push(idx + 1);
            }
        });

        this._scheduleServiceProxy.getEquipSchedules(
            this.filterText,
            days.join(','),
            this.priority,
            this.validTimeStartBegin,
            this.validTimeStartEnd,
            this.validTimeEndBegin,
            this.validTimeEndEnd,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }


    reloadPage(): void {
        this.paginator.changePage(this.paginator.getPage());
    }

    exportToExcel(): void {
    }


    deleteSchedule(schedule: ScheduleListDto): void {
        this.message.confirm(
            '确认删除该假期检修吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    this._scheduleServiceProxy.deleteEquipSchedule(schedule.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }
}
